#! /usr/local/bin/gosh

(use www.cgi)
(use www.contcgi)
(use text.html-lite)
(use math.mt-random)

(define answer (let1 m (make <mersenne-twister> :seed (sys-time))
		 (mt-random-integer m 100)))

(define (main args)
  (contcgi-main
   (lambda (params)
     (let loop ((count 1)
		(guess 0))
       (if (= guess answer)
	   (show `(,(cgi-header)
		   ,(html-doctype)
		   ,(html:html
		     (html:head
		      (html:meta :http-equiv "Content-Type" :content "text/html; charset=utf-8"))
		     (html:body
		      (html:p (html:b (format "pid = ~a" (sys-getpid))))
		      (html:p (format #f "~a 回目で正解です! 答えは ~a" (- count 1) answer))))))
	   (let1 req (show `(,(cgi-header)
			     ,(html-doctype)
			     ,(html:html
			       (html:head
				(html:meta
				 :http-equiv "Content-Type"
				 :content "text/html; charset=utf-8"))
			       (html:body
				(html:p (html:b (format "pid = ~a" (sys-getpid))))
				(html:p (format #f "~a 回目の挑戦です。" count))
				(html:p
				 (cond
				  ((= count 1)
				   (format #f "数当てゲームです。答えは 0 から 99 の間の数です。さて、いくつ?"))
				  ((> guess answer)
				   (format #f "いいえ。 ~a より小さな数です。さて、いくつ?" guess))
				  ((< guess answer)
				   (format #f "いいえ。 ~a より大きな数です。さて、いくつ?" guess))))
				(html:form
				 :method "GET" :action "./guess.fcgi"
				 (html:input :type "hidden" :name "cont-count" :value cont-count)
				 (html:input :type "text" :name "value")
				 (html:input :type "submit"))))))
	     (loop (+ count 1)
		   (cgi-get-parameter "value" req :convert x->integer))))))))

;; Local variables:
;; mode: scheme
;; end:
